# -*- coding: utf-8 -*-
"""
DAL Class 部分
"""

import DBLink.dbHelp
dbHelp = DBLink.dbHelp

def _api_class_add(_request):
    """
     API /api/class.add

    功能：分类 -- 新增
    编写：宋德聪
    题目类型分类，三级（项目如CCIH,CCC|部门如互联网中心|方向如后端），类似数据字典主表
    :param _request: 传入参数 = { "RequestBaseHeader":{"SeesionKey":"","TimeSpan":" "} ,"operationUserID":"","PID":"","DName":"","ExMessage":"" }
    :param _request["operationUserID"]:操作账号
    :param _request["PID"]:PＩＤ
    :param _request["DName"]:节点名称
    :param _request["ExMessage"]:描述信息
    :return: _respon 返回信息 = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "True", "IsError": "False"}
    """

    # 2016年12月7日 09:47:40 测试推送测试
    # 初始化返回信息
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}

    # 01 TreeLevel 取值分析
    cs_this_TreeLevel = -1

    if _request["PID"] == 0:
        cs_this_TreeLevel = 0
    else:
        # 查找上级的TreeLevel
        sql_01_select = """
        SELECT T1.TreeLevel FROM tb_class T1 WHERE T1.MID = '"""+str(_request["PID"])+"""'
        """
        obj_01 = dbHelp.Select(sql_01_select)
        if obj_01["RowNum"] ==0:
            cs_this_TreeLevel = -1
        else:
            cs_parent_TreeLevel = obj_01["Value"][0]["TreeLevel"]
            cs_this_TreeLevel = cs_parent_TreeLevel + 1

    if cs_this_TreeLevel == -1:
        _respon["IsError"] = True
        _respon["ErrorInfo"]["ErrorCode"] = 1
        _respon["ErrorInfo"]["ErrorMsg"] = "数据结构异常，新增执行取消，请与管理员联系！"
        _respon["ReturnValue"] = False
        return _respon
    else:
        pass

    # 02 执行新增
    # 02-01 获取主键ID
    cs_MID = dbHelp.GetNewids("tb_class")
    if cs_MID["Value"] == 0:
        _respon["IsError"] = cs_MID["IsError"]
        _respon["ErrorInfo"]["ErrorCode"] = cs_MID["ErrorCode"]
        _respon["ErrorInfo"]["ErrorMsg"] = cs_MID["ErrorMsg"]
        _respon["ReturnValue"] = cs_MID["ReturnValue"]
    else:
        value_insert = {}
        value_insert["tbaleName"] = "tb_class"
        value_insert["value_normal"] = {}
        value_insert["value_normal"].update({"create_user": _request["operationUserID"]})
        value_insert["value_normal"].update({"MID": cs_MID["Value"]})
        value_insert["value_normal"].update({"PID": _request["PID"]})
        value_insert["value_normal"].update({"DName": _request["DName"]})
        value_insert["value_normal"].update({"TreeLevel": cs_this_TreeLevel})
        value_insert["value_normal"].update({"ExMessage": _request["ExMessage"]})
        value_insert["value_normal"].update({"IsDelete": "0"})

        value_insert["value_special"] = {}
        value_insert["value_special"].update({"create_time": "now()"})

        sql_02_insert = dbHelp.ff_sql_gotten_insert(value_insert)

        obj_02 = dbHelp.Exec(sql_02_insert)
        _respon["IsError"] = obj_02["IsError"]
        _respon["ErrorInfo"]["ErrorCode"] = obj_02["ErrorCode"]
        _respon["ErrorInfo"]["ErrorMsg"] = obj_02["ErrorMsg"]
        _respon["ReturnValue"] = obj_02["ReturnValue"]

    return _respon

def _api_class_edit(_request):
    """
    API /api/class.edit

    功能：分类 -- 编辑
    编写：宋德聪
    题目类型分类，三级（项目如CCIH,CCC|部门如互联网中心|方向如后端），类似数据字典主表
    :param _request: 传入参数 = { "RequestBaseHeader":{"SeesionKey":"","TimeSpan":" "} ,"operationUserID":"","MID":"","PID":"","DName":"","ExMessage":"" ,"PID_Old":"","DName_Old":"","ExMessage_Old":""}
    :param _request["operationUserID"]:操作账号
    :param _request["MID"]:节点ID
    :param _request["PID"]:节点PID
    :param _request["DName"]:节点名称
    :param _request["ExMessage"]:描述信息
    :param _request["PID_Old"]:校对用，旧的节点PID
    :param _request["DName_Old"]:校对用，旧的节点名称
    :param _request["ExMessage_Old"]:校对用，旧的描述信息
    :return: _respon 返回信息 = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "True", "IsError": "False"}
    """
    # 初始化返回信息
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}

    # 因为不提供节点转移功能，TreeLevel判断暂时不做 2016年11月29日 16:42:58

    # 执行编辑操作
    value_update = {}
    value_update["tbaleName"] = "tb_class"

    value_update["set_normal"] = {}
    value_update["set_normal"].update({"update_user": _request["operationUserID"]})
    value_update["set_normal"].update({"PID": _request["PID"]})
    value_update["set_normal"].update({"DName": _request["DName"]})
    value_update["set_normal"].update({"ExMessage": _request["ExMessage"]})

    value_update["set_special"] = {}
    value_update["set_special"].update({"update_time": "now()"})

    value_update["where_normal"] = {}
    value_update["where_normal"].update({"MID": _request["MID"]})
    value_update["where_normal"].update({"PID": _request["PID_Old"]})
    value_update["where_normal"].update({"DName": _request["DName_Old"]})
    value_update["where_normal"].update({"ExMessage": _request["ExMessage_Old"]})

    value_update["where_special"] = {}

    sql_01_update = dbHelp.ff_sql_gotten_update(value_update)

    obj_01 = dbHelp.Exec(sql_01_update)
    _respon["IsError"] = obj_01["IsError"]
    _respon["ErrorInfo"]["ErrorCode"] = obj_01["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = obj_01["ErrorMsg"]
    _respon["ReturnValue"] = obj_01["ReturnValue"]


    return _respon


def _api_class_delete(_request):
    """
    API /api/class.delete

    功能：分类 -- 逻辑删除
    编写：宋德聪
    题目类型分类，三级（项目如CCIH,CCC|部门如互联网中心|方向如后端），类似数据字典主表
    :param _request: 传入参数 = { "RequestBaseHeader":{"SeesionKey":"","TimeSpan":" "} ,"operationUserID":"","MID":""}
    :param _request["operationUserID"]:操作账号
    :param _request["MID"]:节点ID
    :return: _respon 返回信息 = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "True", "IsError": "False"}
    """
    # 初始化返回信息
    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}

    value_update = {}
    value_update["tbaleName"] = "tb_class"

    value_update["set_normal"] = {}
    value_update["set_normal"].update({"update_user": _request["operationUserID"]})
    value_update["set_normal"].update({"IsDelete": 1})

    value_update["set_special"] = {}
    value_update["set_special"].update({"update_time": "now()"})

    value_update["where_normal"] = {}
    value_update["where_normal"].update({"MID": _request["MID"]})

    value_update["where_special"] = {}

    sql_01_update = dbHelp.ff_sql_gotten_update(value_update)

    obj_01 = dbHelp.Exec(sql_01_update)
    _respon["IsError"] = obj_01["IsError"]
    _respon["ErrorInfo"]["ErrorCode"] = obj_01["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = obj_01["ErrorMsg"]
    _respon["ReturnValue"] = obj_01["ReturnValue"]

    return _respon

def _api_class_view_tree(_request):
    """
    API /api/class.view.tree

    功能：分类 -- 查询 -- 全部信息（目录树）
    编写：宋德聪
    题目类型分类，三级（项目如CCIH,CCC|部门如互联网中心|方向如后端），类似数据字典主表
    :param _request: 传入参数 = { "RequestBaseHeader":{"SeesionKey":"","TimeSpan":" "} }
    :return: _respon 返回信息 = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "True", "IsError": "False","RowNum":"","date_list":"","zTreeData":""}
    :param _respon["RowNum"]:查询返回记录数量
    :param _respon["date_list"]:正常返回的查询结果
    :param _respon["zTreeData"]:根据前端要求进行处理后的查询结果
    """

    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}

    sql_01_select = """
        SELECT
         MID
        ,PID
        ,DName
        ,TreeLevel
        ,ExMessage
        FROM
         tb_class T1
        WHERE
         T1.IsDelete = 0
    """
    obj_01 = dbHelp.Select(sql_01_select)
    _respon["ErrorInfo"]["ErrorCode"] = obj_01["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = obj_01["ErrorMsg"]
    _respon["IsError"] = obj_01["IsError"]
    _respon["ReturnValue"] = obj_01["ReturnValue"]
    _respon["RowNum"] = obj_01["RowNum"]

    if obj_01["RowNum"]==0:
        #  没有查询到结果
        pass
    else:
        # 正常返回的查询结果
        _respon["date_list"] = obj_01["Value"]
        # 根据前端要求对数据进行处理后的返回
        csa = "["
        for x in obj_01["Value"]:
            csa = csa + '{id:' + str(x['MID']) + ',pId:' + str(x['PID']) + ',name:"' + str(x['DName']) + '"},'
        csa = csa + "]"
        _respon["zTreeData"] = csa

    return _respon

def _api_class_view_one(_request):
    """
    API /api/class.view.one

    功能：分类 -- 查询 -- 单记录信息查询
    编写：宋德聪
    题目类型分类，三级（项目如CCIH,CCC|部门如互联网中心|方向如后端），类似数据字典主表
    :param _request: 传入参数 = { "RequestBaseHeader":{"SeesionKey":"","TimeSpan":" "},"MID":""}
    :param _request["MID"]:节点ID
    :return: _respon 返回信息 = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "True", "IsError": "False","RowNum":"","date_list":""}
    :param _respon["date_list"]:正常返回的查询结果
    :param _respon["date_list"][0]["MID"]:MID
    :param _respon["date_list"][0]["PID"]:PID
    :param _respon["date_list"][0]["DName"]:阶段名称
    :param _respon["date_list"][0]["TreeLevel"]:节点层次
    :param _respon["date_list"][0]["ExMessage"]:描述信息
    """

    _respon = {"ErrorInfo": {"ErrorCode": "0", "ErrorMsg": ""}, "ReturnValue": "", "IsError": ""}

    sql_01_select = """
            SELECT
             MID
            ,PID
            ,DName
            ,TreeLevel
            ,ExMessage
            FROM
             tb_class T1
            WHERE
             T1.IsDelete = 0
            AND
			 T1.MID = '"""+str(_request["MID"])+"""'
        """
    obj_01 = dbHelp.Select(sql_01_select)
    _respon["ErrorInfo"]["ErrorCode"] = obj_01["ErrorCode"]
    _respon["ErrorInfo"]["ErrorMsg"] = obj_01["ErrorMsg"]
    _respon["IsError"] = obj_01["IsError"]
    _respon["ReturnValue"] = obj_01["ReturnValue"]
    _respon["RowNum"] = obj_01["RowNum"]
    if obj_01["RowNum"]==0:
        #  没有查询到结果
        pass
    else:
        # 正常返回的查询结果
        _respon["date_list"] = obj_01["Value"]

    return _respon